<?php

    class InboxController extends Application {
        
        function setup()
        {
            $this->actions = array('index','leads','modify','delete','modify_responder',
                                   'download_leads','view_lead','delete_lead','modify_plugins');
            $this->use_layout('admin_layout.php');
            $this->before_filter('is_logged_in');
            $this->before_filter('has_access');
            use_model('inbox');
        }
        
        function has_access()
        {
            if (!has_access_to('Inbox::Access Inbox')) {
                flash('error','Sorry but you do not have access to the Inbox tool.');
                $this->redirect_to(array('controller'=>'admin/dashboard'));
            }
        }
        
		function index() 
		{
		    // get report of all forms in system
		    $inbox = new Inbox();		
		    $this->assign_ref('report',$inbox->get_paged_forms());
		}
		
		function leads()
		{
		    // sorting
		    if ( isset($_GET['browse']) )
		    {		        
		        // clean up values then pass them to get_paged_leads()
		        $browse['inbox_form_id'] = (int) $_GET['inbox_form_id'] ;
		        $browse['month']         = (int) $_GET['date']['mon'];
		        $browse['year']          = (int) $_GET['date']['year'];
		        $browse['day']           = (int) $_GET['date']['mday'];
		        $_SESSION['browse']      = $browse;
		    }
		    
		    if ( empty($_SESSION['browse']) )
		    {
		        $_SESSION['browse'] = array();
		    }
		    
		    if ( isset($_GET['view_all']) )
		    {
		        $_SESSION['browse'] = array();
		    }

		     // get report of all forms in system
     	    use_model('inbox_leads');
		    $inbox_leads = new Inbox_Leads();
		    $this->assign_ref('report',$inbox_leads->get_paged_leads(sanitize($_SESSION['browse'])));
		    
		    $inbox = new Inbox();
		    $this->assign('current_forms',$inbox->get_all_forms());
		}
		
		function download_leads()
		{
		    $this->use_layout(false);
		    use_model('inbox_leads');
		    $inbox_leads = new Inbox_Leads();
		    if ( isset($_GET['id']) )
		    {
		        $report = $inbox_leads->get_report( (int) $_GET['id']);
		    } else {
		        $report = $inbox_leads->get_report();
		    }
		    
		    header("Content-Type: application/octet-stream");
            header("Content-disposition:attachment; filename=\"leads_export_".date("YmdHis").".csv\"");      
            echo $report;
            return;
	    }
		
		function modify()
		{
		    if ( !has_access_to('Inbox::Add Forms') || !has_access_to('Inbox::Edit Forms') )
            {
                $this->redirect_to('index');
            }
		    $inbox = new Inbox();
		    $this->assign_ref('inbox',$inbox);
		    if ( !empty($_GET['id']) )
		    {
		        if ( $inbox->load( (int) $_GET['id'] ) )
		        {
		            $this->assign('form',$inbox->fields);
		            $plugins = unserialize($inbox->get('plugins'));
		            if(!$plugins) $plugins = array();
		            $this->assign('plugins',$plugins);
		            $this->assign('all_plugins',$this->_get_plugins());
		        }
		    }
		    if ( isset($_POST['action']) && $_POST['action'] == 'modify_form' )
		    {
		        if ( isset($_POST['inbox']['id']) &&!has_access_to('Inbox::Add Forms') ) {
		            $this->redirect_to('index');
		            return;
		        } elseif (!isset($_POST['inbox']['id']) &&!has_access_to('Inbox::Edit Forms')) {
		            $this->redirect_to('index');
		            return;
		        }
		        
		        $inbox->load( (int) $_POST['inbox']['id']);    	    
    		    if ( $inbox->update_fields($_POST['inbox']))
    	        {		            
    	            flash('notice','Form Saved');
    	            $this->redirect_to(array('controller'=>'admin/inbox' ,'action'=>'modify?id='.$inbox->get('id')));		            		            
    	        } else {
    	            flash('error','Form failed to save');
    	        }
		    }
		    		    
		}
		
		
		function modify_plugins()
		{
		    if ( !has_access_to('Inbox::Add Forms') || !has_access_to('Inbox::Edit Forms') )
            {
                $this->redirect_to('index');
            }
            $inbox = new Inbox();
            $inbox->load( (int) $_POST['id']);
            $inbox->set('plugins', serialize($_POST['plugins']));
    		if ( $inbox->save())
	        {		            	            
                $this->render(array('inline'=>'Plugins Saved'));
	        } else {
	            $this->render(array('inline'=>'Plugins could not be saved'));
	        }
		}
		
		function modify_responder()
		{
		    if ( !has_access_to('Inbox::Edit Forms') )
		    {
		        $this->redirect_to('index');
		        return;
		    }
            if ( empty($_POST['inbox']['autoresponder']) )
            {
                return;
            }
            $inbox = new Inbox();
            if ( $inbox->load( (int) $_POST['inbox']['id']) 
	            && $inbox->update_fields($_POST['inbox']))
	        {		            
	            $value = htmlspecialchars(string_trim($inbox->get('autoresponder')));
	            $js = "<script language=\"javascript\" type=\"text/javascript\">$('inbox_autoresponder').value = \"$value\";</script>";
	            $this->render(array('inline'=>'Autoresponder Saved '.$js));
            }

		}
		
		function delete()
		{
		    if ( $_POST && has_access_to('Inbox::Delete Forms'))
		    {
		        $inbox = new Inbox();
		        $id = (int) $_POST['id'];
		        if ( $inbox->load($id) )
		        {
		            $inbox->delete();
		            flash('notice','Form Deleted');
		        }
		    }
		    $this->redirect_to('index');
		}
        
        function view_lead()
        {
            if ( isset($_GET['id']) )
            {   
                use_model('inbox_leads');
                $id = (int) $_GET['id'];
                $leads = new Inbox_Leads();
                $leads->load($id);
                $this->assign('lead',$leads->get_viewable_lead());
            } else {
                $this->redirect_to('index');
            }
        }
        
        function delete_lead()
        {
            if ( $_POST && !empty($_POST['id']) )
            {
                use_model('inbox_leads');
                $id = (int) $_POST['id'];
                $leads = new Inbox_Leads();
                $leads->load($id);
                $leads->delete();
                flash('notice','Lead Deleted');
                $this->redirect_to('leads');
            } else {
                $this->redirect_to('leads');
            }
        }
        
        function _get_plugins()
		{
		    $plugins = array();
            $files = glob(PLUGINS.'/*.php');            
            if(!empty($files)){
                foreach($files as $f){
                    // get description of plugin
                    #$string = file_get_contents($f);
                    #preg_match("|Description:(.*)|i", $string, $description);
                    $name   = str_replace(PLUGINS.'/','',$f);
                    $name   = str_replace('.php','',$name);
                    #$plugins[$name] = $description[1];
                    $plugins[$name] = $name;
                }
                ksort($plugins);
                return $plugins;
            } else {
                return false;   
            }
		}
        
    }

?>